//时间验证 参数格式为YYYY-MM-DD
function dateCompare(dateStart, dataEnd) {
    var flag = 0;
    var start = new Date(dateStart).getTime();
    var end = new Date(dataEnd).getTime();
    if (end < start) {
        flag = 1;
    }
    return flag;
}
//画横线
function horizonLine(warningValue, time) {
    var warning = [];
    var i = 0;
    while (i < time.length) {
        var warningItem = [];
        warningItem.push(time[i]);
        warningItem.push(warningValue);
        warning.push(warningItem);
        i++;
    }
    return warning;
}

// Start Painting TargetPic
function paintPic(data, option, warningFlag) {

    var dataTotal = [];
    var seriesInfo = [];
    if (option == 'mainBeamDeflection' || option == 'maintowerDeformation') {

        for (var k = 0; k < data.length; k++) {

            var dataAvg = [];
            var dataMax = [];
            var dataMin = [];

            var i = 0;
            while (i < data[k].time.length) {
                var dataAvgItem = [],
                    dataMaxItem = [],
                    dataMinItem = [];

                dataAvgItem.push(data[k].time[i]);
                dataAvgItem.push(data[k].avg[i]);

                dataMaxItem.push(data[k].time[i]);
                dataMaxItem.push(data[k].max[i]);

                dataMinItem.push(data[k].time[i]);
                dataMinItem.push(data[k].min[i]);

                dataAvg.push(dataAvgItem);
                dataMax.push(dataMaxItem);
                dataMin.push(dataMinItem);

                i++;
            }
            dataTotal.push(dataAvg);
            dataTotal.push(dataMax);
            dataTotal.push(dataMin);
            seriesInfo.push({ id: 'avg' + data[k].sensorId, name: '平均值' + data[k].sensorId, data: dataTotal[3 * k + 0] });
            seriesInfo.push({ id: 'max' + data[k].sensorId, name: '最大值' + data[k].sensorId, data: dataTotal[3 * k + 1] });
            seriesInfo.push({ id: 'min' + data[k].sensorId, name: '最小值' + data[k].sensorId, data: dataTotal[3 * k + 2] });

            if (option == 'mainBeamDeflection' && warningFlag) {
                seriesInfo.push({ id: 'warning' + data[k].sensorId, name: '预警值' + data[k].sensorId, data: horizonLine(data[k].warning, data[k].time) });
            }
            if (option == 'maintowerDeformation') {
                if (warningFlag[0]) {
                    seriesInfo.push({ id: 'yellow' + data[k].sensorId, name: '黄色预警值' + data[k].sensorId, data: horizonLine(data[k].yellowWarning, data[k].time) });

                }
                if (warningFlag[1]) {
                    seriesInfo.push({ id: 'red' + data[k].sensorId, name: '红色预警值' + data[k].sensorId, data: horizonLine(data[k].redWarning, data[k].time) });
                }
            }
        }

    } else {
        for (var i = 0; i < data.length; i++) {
            var dataSingle = [];
            var j = 0;
            while (j < data[i].time.length) {
                dataItem = [];
                dataItem.push(data[i].time[j]);
                switch (option) {
                    case 'offset':
                        dataItem.push(data[i].data[j]);
                        break;
                    case 'fissure':
                        dataItem.push(data[i].data[j]);
                        break;
                    case 'boxGirderCrack':
                        dataItem.push(data[i].data[j]);
                        break;
                    case 'slingCableforce':
                        dataItem.push(data[i].data[j]);
                        break;
                    case 'steelBoxGirderStrain':
                        dataItem.push(data[i].data[j]);
                        break;
                    case 'maincableForce':
                        dataItem.push(data[i].data[j]);
                        break;
                }
                dataSingle.push(dataItem);
                j++;
            }
            dataTotal.push(dataSingle);
            var seriesSingle = {
                id: i,
                name: data[i].sensorId,
                data: dataTotal[i]
            };
            seriesInfo.push(seriesSingle);
            if (warningFlag) {
                seriesInfo.push({ id: 'warining' + data[i].sensorId, name: '预警值' + data[i].sensorId, data: horizonLine(data[i].warning, data[i].time) });
            }
        }
    }
    console.log("seriesInfo: " + seriesInfo);
    console.log("dataTotal: " + dataTotal);

    Highcharts.stockChart('result_pic', {
        rangeSelector: {
            selected: 2
        },

        title: {
            text: ''
        },
        plotOptions: {
            series: {
                showInLegend: true
            }
        },
        legend: {
            enabled: true,
        },
        series: seriesInfo
    });
    $("#result_pic").addClass('in');
}

/**
 * 图表文本中文化
 */
Highcharts.setOptions({
    lang: {
        contextButtonTitle: "图表导出菜单",
        decimalPoint: ".",
        downloadJPEG: "下载JPEG图片",
        downloadPDF: "下载PDF文件",
        downloadPNG: "下载PNG文件",
        downloadSVG: "下载SVG文件",
        invalidDate: "无效的时间",
        loading: "加载中...",
        months: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        noData: "没有数据",
        numericSymbols: null,
        printChart: "打印图表",
        resetZoom: "恢复缩放",
        resetZoomTitle: "恢复图表",
        shortMonths: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        thousandsSep: ",",
        weekdays: ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"],
        rangeSelectorFrom: "开始时间",
        rangeSelectorTo: "结束时间",
        rangeSelectorZoom: "范围",
        zoomIn: "缩小",
        zoomOut: "放大"
    },

    rangeSelector: {

        allButtonsEnabled: true,
        selected: 2,
        inputDateFormat: "%Y-%m-%d",
        buttonTheme: {
            width: "auto",
            style: { fontSize: "12px", padding: "4px" }
        },
        buttons: [{ type: "month", count: 1, text: "月" },
            { type: "month", count: 3, text: "季度" },
            { type: "month", count: 6, text: "半年" },
            { type: "ytd", text: "YTD" },
            { type: "year", count: 1, text: "年" },
            { type: "all", text: "所有" }
        ]
    },

    xAxis: {
        type: 'datetime',
        labels: {
            format: '{value:%m-%d}',
            rotation: 0,
            align: 'center'
        }
    },
    tooltip: {
        split: false,
        shared: true,
        xDateFormat: '%Y-%m-%d',

    },
    credits: {
        enabled: false
    },
    exporting: {
        enabled: false
    }
});


function loadSensorList(data) { //动态加载传感器列表
    $("#sensor").empty();
    var sensor_var = $("#sensor");

    if (sensor_var) {
        for (var i = 0; i < data.data.length; i++) {
            sensor_var.append("<option value='" + data.data[i].val + "'>" + data.data[i].val + "</option>");
        }
    }
    $('.selectpicker').selectpicker('val', data.data[0].val);
    $('.selectpicker').selectpicker('refresh');
}




function addButton() {
    console.log("addbutton");
    var select_ul = new Array();
    for (var i = 0; i < 4; i++) { // i<4  是因为 数据下载页面有4个选择框
        select_ul[i] = document.getElementsByClassName("dropdown-menu inner")[i];
        if (select_ul[i]) {
            var newli = document.createElement("li");
            var newbutton = document.createElement("button");

            newbutton.setAttribute("type", "button");
            newbutton.setAttribute("class", "btn btn-primary");
            newbutton.setAttribute("id", "makesure");
            newbutton.style.cssFloat = "right";
            newbutton.innerHTML = "确认";

            newli.appendChild(newbutton);
            select_ul[i].appendChild(newli);
        }

    }
}

function disabled() { //按下确认按钮图片还没加载完时禁用各种输入
    $('input').attr('disabled', 'disabled');
    $('select').attr('disabled', 'disabled');
    $('button').attr('disabled', 'disabled');
}

function enabled() { //图片加载完后启用各种输入
    $("input").removeAttr("disabled");
    $("select").removeAttr("disabled");
    $("button").removeAttr("disabled");
}